package com.qs.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qs.controller.vo.FormDataVO;
import com.qs.mapper.entity.FormData;
import com.qs.service.FormDataService;
import com.qs.service.bo.FormDataBO;
import com.qs.util.R;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/formData")
@RequiredArgsConstructor
public class FormDataController {

    private final FormDataService formDataService;

    @GetMapping("{formId}")
    private R<Page<List<FormData>>> queryFormData(@PathVariable("formId") Long formId, @RequestParam("current") Long current, @RequestParam("size") Long size, @RequestParam("keyword") String keyword) {
        Page<List<FormData>> result = formDataService.queryFormData(formId, current, size, keyword);
        return R.ok(result);
    }

    @PostMapping
    private R<String> add(@RequestBody FormDataVO formDataVO) {
        String result = formDataService.add(formDataVO);
        return R.ok(result);
    }

    @PutMapping
    private R<String> edit(@RequestBody FormDataVO formDataVO) {
        String result = formDataService.edit(formDataVO);
        return R.ok(result);
    }

    @DeleteMapping("/{rowId}")
    private R<String> delete(@PathVariable("rowId") String rowId) {
        LambdaQueryWrapper<FormData> formDataLambdaQueryWrapper = Wrappers.lambdaQuery();
        formDataLambdaQueryWrapper.eq(FormData::getRowId, rowId);
        boolean removed = formDataService.remove(formDataLambdaQueryWrapper);
        String result = removed ? "删除成功" : "删除失败";
        return R.ok(result);
    }

    @PostMapping("/batchDelete")
    private R<String> batchDelete(@RequestBody List<String> rowIdList) {
        for (String rowId : rowIdList) {
            LambdaQueryWrapper<FormData> formDataLambdaQueryWrapper = Wrappers.lambdaQuery();
            formDataLambdaQueryWrapper.eq(FormData::getRowId, rowId);
            boolean removed = formDataService.remove(formDataLambdaQueryWrapper);
        }
        return R.ok("删除成功");
    }
}
